<template>
  <div>
    <Mytable
      :cols="cols"
      :userlist="userlist"
      :pageList="pageList"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    >
      <template v-slot:statu="{ row, index }">
        <el-switch
          v-model="row.mg_state"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="state(row)"
        >
        </el-switch>
      </template>
      <template v-slot:handle="{ row, index }">
        <el-button
          type="primary"
          icon="el-icon-edit"
          @click="edit(row)"
        ></el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          @click="del(row)"
        ></el-button>
        <el-button
          type="warning"
          icon="el-icon-s-tools"
          @click="status1(row)"
        ></el-button>
      </template>
    </Mytable>
    <!-- 编辑模态框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible1" width="30%">
      <el-form
        :model="Form"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="Form.username" disabled></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="Form.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="Form.mobile"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="editform">立即创建</el-button>
          <el-button @click="resetForm('Form')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import Mytable from "../components/Mytable";
import { users, deleteUser, editok } from "../utils/api";
export default {
  components: {
    Mytable,
  },
  data() {
    return {
      total: 0, //条数
      dialogVisible1: false, //编辑弹出框
      Form: {}, //编辑数据
      userlist: [],
      pageList: {
        pagenum: 1,
        pagesize: 3,
      },
      cols: [
        {
          type: "index",
        },
        {
          labelName: "姓名",
          propsVal: "username",
          width: "150px",
        },
        {
          labelName: "邮箱",
          propsVal: "email",
          width: "150px",
        },
        {
          labelName: "电话",
          propsVal: "mobile",
          width: "150px",
        },
        {
          labelName: "角色",
          propsVal: "role_name",
          width: "170px",
        },
        {
          labelName: "状态",
          slot: "statu",
          width: "170px",
        },
        {
          labelName: "操作",
          slot: "handle",
          width: "170px",
        },
      ],
    };
  },
  methods: {
    // 数据
     //   分页
      handleSizeChange(val) {
        console.log(val,'size change');
        console.log(this.pageList);
        this.getList()
      },
      handleCurrentChange(val) {
        console.log(val,'num');
        console.log(this.pageList);
        this.getList()
      },
    getList() {
      users(this.pageList).then((res) => {
        this.userlist = res.data.data.users;
        console.log(this.userlist);
      });
    },
    // 编辑
    edit(row) {
      this.dialogVisible1 = true;
      this.Form = JSON.parse(JSON.stringify(row));
    },
    // 确定编辑
    editform() {
      console.log(this.Form);
      editok(this.Form).then((res) => {
        console.log(res.data);
        this.getList();
        this.dialogVisible1 = false;
        if (res.data.meta.status == 200) {
          this.$message({
            showClose: true,
            message: "编辑成功",
            type: "success",
          });
        }
      });
    },
    // 删除
    del(row) {
      console.log(row);
      deleteUser(row.id).then((res) => {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            console.log(res);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      });
    },
  },
  created() {
    this.getList();
  },
  mounted() {},
  computed: {},
  watch: {},
};
</script>

<style lang="scss" scoped></style>
